package com.snowfield.lb47.lbconfig.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.snowfield.lb47.lbconfig.domain.HttpCheckMethod;
import com.snowfield.lb47.lbconfig.service.IHttpCheckMethodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * HTTP检测方法Controller
 *
 * @author snowfield
 * @date 2024-05-28
 */
@RestController
@RequestMapping("/lbconfig/checkmethod")
public class HttpCheckMethodController extends BaseController {
    @Autowired
    private IHttpCheckMethodService httpCheckMethodService;

    /**
     * 查询HTTP检测方法列表
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:checkmethod:list')")
    @GetMapping("/list")
    public TableDataInfo list(HttpCheckMethod httpCheckMethod) {
        startPage();
        List<HttpCheckMethod> list = httpCheckMethodService.selectHttpCheckMethodList(httpCheckMethod);
        return getDataTable(list);
    }

    /**
     * 导出HTTP检测方法列表
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:checkmethod:export')")
    @Log(title = "HTTP检测方法", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HttpCheckMethod httpCheckMethod) {
        List<HttpCheckMethod> list = httpCheckMethodService.selectHttpCheckMethodList(httpCheckMethod);
        ExcelUtil<HttpCheckMethod> util = new ExcelUtil<HttpCheckMethod>(HttpCheckMethod.class);
        util.exportExcel(response, list, "HTTP检测方法数据");
    }

    /**
     * 获取HTTP检测方法详细信息
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:checkmethod:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(httpCheckMethodService.selectHttpCheckMethodById(id));
    }

    /**
     * 新增HTTP检测方法
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:checkmethod:add')")
    @Log(title = "HTTP检测方法", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HttpCheckMethod httpCheckMethod) {
        if (!httpCheckMethodService.checkNameUnique(httpCheckMethod)) {
            return AjaxResult.error("新增检测方法 '" + httpCheckMethod.getMethodName() + "'失败，名称已经存在");
        }
        return toAjax(httpCheckMethodService.insertHttpCheckMethod(httpCheckMethod));
    }

    /**
     * 修改HTTP检测方法
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:checkmethod:edit')")
    @Log(title = "HTTP检测方法", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HttpCheckMethod httpCheckMethod) {
        if (!httpCheckMethodService.checkNameUnique(httpCheckMethod)) {
            return AjaxResult.error("修改检测方法 '" + httpCheckMethod.getMethodName() + "'失败，名称已经存在");
        }
        return toAjax(httpCheckMethodService.updateHttpCheckMethod(httpCheckMethod));
    }

    /**
     * 删除HTTP检测方法
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:checkmethod:remove')")
    @Log(title = "HTTP检测方法", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(httpCheckMethodService.deleteHttpCheckMethodByIds(ids));
    }
}
